package com.ruoyi.iot.client.listener;

import com.ruoyi.device.api.enums.DeviceStatusEnum;
import com.ruoyi.iot.dataprocessing.service.AsyncService;
import net.dreamlu.iot.mqtt.spring.client.MqttClientSubscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 客户端消息监听
 *
 * @author L.cm
 */
@Service
public class MqttClientSubscribeListener {
	private static final Logger logger = LoggerFactory.getLogger(MqttClientSubscribeListener.class);
	@Autowired
	private AsyncService asyncService;

	@MqttClientSubscribe("$queue//mqtt/device/message/#")
	public void subQos0(String topic, byte[] payload) {
		asyncService.executeAsync(topic,payload);
//		logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
	}

	@MqttClientSubscribe("$queue//mqtt/device/online")
	public void online(String topic, byte[] payload) {

		asyncService.updateDeviceStatus(new String(payload), DeviceStatusEnum.ON_LINE.getStatus());
	}


	@MqttClientSubscribe("$queue//mqtt/device/offline")
	public void offline(String topic, byte[] payload) {
		asyncService.updateDeviceStatus(new String(payload),DeviceStatusEnum.OFF_LINE.getStatus());
	}

	/*@MqttClientSubscribe(value = "/qos1/#", qos = MqttQoS.QOS1)
	public void subQos1(String topic, byte[] payload) {
		logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
	}

	@MqttClientSubscribe("/sys/${productKey}/${deviceName}/thing/sub/register")
	public void thingSubRegister(String topic, byte[] payload) {
		// 1.3.8 开始支持，@MqttClientSubscribe 注解支持 ${} 变量替换，会默认替换成 +
		// 注意：mica-mqtt 会先从 Spring boot 配置中替换参数 ${}，如果存在配置会优先被替换。
		logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
	}*/

}

